import * as API from "../api";
import Base from "./base";

class Acticle extends Base {
  get state() {
    return {
      acticleList: []
    };
  }
  get actions() {
    return {
      // 获取列表
      ACTICLE_LIST_ACTION: async store => {
        try {
          let result = await this.get(API.ACTICLES);
          if (result && result.code === 1) {
            store.commit("ACTICLE_LIST_COMMIT", result.data.list);
          }
          return result;
        } catch (error) {
          console.error(error);
        }
      },
      // 获取单个
      ACTICLE_ACTION: async (store, payload) => {
        try {
          let result = await this.get(API.ACTICLES + "/" + payload);
          if (result && result.code === 1) {
            return result.data;
          }
        } catch (error) {
          console.error(error);
        }
      },
      //新建文章
      ACTICLE_CREATE_ACTION: async (store, payload) => {
        try {
          let result = await this.post(API.ACTICLES, payload);
          if (result && result.code === 1) {
            this.notify(1, "新增文章成功！");
          }
          return result;
        } catch (error) {
          console.error(error);
        }
      },
      //更新文章
      ACTICLE_UPDATE_ACTION: async (store, payload) => {
        try {
          let result = await this.put(API.ACTICLES, payload);
          if (result && result.code === 1) {
            this.notify(1, "修改文章成功！");
          }
          return result;
        } catch (error) {
          console.error(error);
        }
      },
      //删除文章
      ACTICLE_DELETE_ACTION: async (store, payload) => {
        try {
          let result = await this.delete(API.ACTICLES + "/" + payload);
          if (result && result.code === 1) {
            this.notify(1, "删除文章成功！");
            return result;
          }
        } catch (error) {
          console.error(error);
        }
      }
    };
  }
  get mutations() {
    return {
      ACTICLE_LIST_COMMIT(state, payload) {
        state.acticleList = payload;
      }
    };
  }
  get getters() {
    return {
      acticleList: state => state.acticleList
    };
  }
}

export default new Acticle();
